package com.i8office.android.zhihuixinwu.app;

import android.os.Handler;
import android.text.TextUtils;

import com.cyan.tools.util.SPUtils;
import com.i8office.android.zhihuixinwu.dagger.OfficeComponent;
import com.socks.library.KLog;

import application.AppApplication;

/**
 * Created by pc on 2018/4/27.
 */

public class App extends AppApplication {
    public static App mInstance;
    public static OfficeComponent testComponent;
    private String token;

    @Override
    public void onCreate() {
        super.onCreate();
        testComponent = OfficeComponent.OfficeInitialize.init();
        mInstance = this;
        KLog.init(true);
//        Thread.setDefaultUncaughtExceptionHandler(handler);
    }


    Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
//            writeErrorLog(ex);
//            Intent intent = new Intent(getApplicationContext(),
//                    CollapseActivity.class);
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            startActivity(intent);
            KLog.e("systemError:" + ex == null ? ex.toString() : "null");
            // 杀死该应用进程
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    android.os.Process.killProcess(android.os.Process.myPid());
                    System.exit(0);
                }
            }, 1000);
        }
    };

    public static App getApp() {
        if (mInstance != null && mInstance instanceof App) {

            return mInstance;
        } else {
            mInstance = new App();
            mInstance.onCreate();
            return mInstance;
        }
    }

    public String getToken() {
        if (TextUtils.isEmpty(token))
            token = (String) SPUtils.get(this, "Authorization", App.getSession());
        return token;
    }

    public void setToken(String token) {
        SPUtils.put(this, "Authorization", TextUtils.isEmpty(token) ? token : "Bearer " + token);
        this.token = TextUtils.isEmpty(token) ? token : "Bearer " + token;
    }

    public static OfficeComponent getComponent() {
        return testComponent;
    }

}
